<?php
class Pagecontroller extends Controller{
	function Pagecontroller()
	{
		parent::Controller();
	}
	
	function show_page()
    {
        $page_uri           = $this->uri->segment(2);
        $server_file_path   = APPPATH.'/views/pages/'.$page_uri.'.php';
        if (file_exists($server_file_path))
        {
            $data = array(
                            'static_page' => $page_uri
                         );
          $this->load->view('pages/show_page.php', $data);  
		}
        else
        {
        	show_404();
        }  
    }

	function status_msg()
	{
		if (!$this->userland->logged_in() || !$this->userland->current_user()->is_admin)
		{
			show_404();
			exit();
		}
		
		if ($this->input->post('msg') !== false)
		{
			// postback
			$this->db->query('update meta_settings set value = ? where name = ? limit 1', array($this->input->post('msg'), 'site_status'));
		}
		$query 			= $this->db->query('select value from meta_settings where name = ? limit 1', array('site_status'));
		$data['msg'] 	= $query->first_row()->value;
		$this->load->view('pages/status_msg', $data);
	}
	
	function admin_userlist()
	{
		if (!$this->userland->logged_in() || !$this->userland->current_user()->is_admin)
		{
			show_404();
			exit();
		}
		
		$this->load->database();
		$this->load->helper('date');
		
		// load stats
		$stats['user_count'] 	= $this->db->count_all('users');
		$stats['entry_count'] 	= $this->db->count_all('contents');
		$stats['box_count'] 	= $this->db->count_all('bricaboxes');
		$stats['last_signup'] 	= $this->db->query('select login,created_at from users order by created_at desc limit 1')->first_row();
		$stats['latest_box'] 	= $this->db->query('select subdomain,name,created_at from bricaboxes order by created_at desc limit 1')->first_row();
		
		// temp.
		$stats['all_box'] = $this->db->query('select id,name,subdomain,custom_domain,use_custom_domain from bricaboxes order by name asc');
		$stats['all_usr'] = $this->db->query('select * from users order by login asc');
		
		// render the view
		$data = array('static_page' => 'admin_listall', 'stats' => $stats);
		$this->load->view('pages/show_page.php', $data);
	}
	
	function admin_stats()
	{
		if (!$this->userland->logged_in() || !$this->userland->current_user()->is_admin)
		{
			show_404();
			exit();
		}
		
		$this->load->database();
		$this->load->helper('date');
		
		// load stats
		$stats['user_count'] 	= $this->db->count_all('users');
		$stats['entry_count'] 	= $this->db->count_all('contents');
		$stats['box_count'] 	= $this->db->count_all('bricaboxes');
		$stats['last_signup'] 	= $this->db->query('select login,created_at from users order by created_at desc limit 1')->first_row();
		$stats['latest_box'] 	= $this->db->query('select subdomain,name,created_at from bricaboxes order by created_at desc limit 1')->first_row();
		
		// render the view
		$data = array('static_page' => 'admin_stats', 'stats' => $stats);
		$this->load->view('pages/show_page.php', $data);
	}
	
	function user_section()
	{
		if ($this->userland->logged_in())
		{
			$user = $this->userland->current_user();
			echo 'Welcome back <a href="/person/'.$user->login.'">'.username($user).'</a>. Not you? <a href="/log_out">Log out</a>.';
		}
		else
		{
			echo '<span>Hi! <a href="/sign_up_or_log_in">sign up or log in</a>&mdash;it only takes 30 seconds.</span>';
		}
	}
}
?>
